package com.grablist.feihu.grablist.ui.actviity.login;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.grablist.feihu.grablist.R;
import com.grablist.feihu.grablist.base.BaseActivity;
import com.grablist.feihu.grablist.bean.CityInfo;
import com.grablist.feihu.grablist.bean.SettingInfo;
import com.grablist.feihu.grablist.comm.Constants;
import com.grablist.feihu.grablist.comm.MyApplication;
import com.grablist.feihu.grablist.event.NetErrorEvent;
import com.grablist.feihu.grablist.event.RemoveInfoEvent;
import com.grablist.feihu.grablist.ui.actviity.login.biz.SelectInfoBiz;
import com.grablist.feihu.grablist.ui.actviity.login.component.DaggerSetecltInfoComponent;
import com.grablist.feihu.grablist.ui.actviity.login.contract.SelectInfoContract;
import com.grablist.feihu.grablist.ui.actviity.login.module.SelectInfoModule;
import com.grablist.feihu.grablist.ui.actviity.login.presenter.SelectInfoPresenter;
import com.grablist.feihu.grablist.ui.adapter.WirelessAdp;
import com.grablist.feihu.grablist.ui.widget.ListViewForScrollView;
import com.grablist.feihu.grablist.util.PreferencesUtils;
import com.grablist.feihu.grablist.util.StringUtils;
import com.jakewharton.rxbinding.view.RxView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.Bind;
import rx.functions.Action1;

public class SelcetInfoAct extends BaseActivity implements SelectInfoContract.View {

    @Bind(R.id.needsx)
    TextView needsx;
    @Bind(R.id.select_info_city)
    LinearLayout business;
    @Bind(R.id.select_info_custom)
    TextView education;
    @Bind(R.id.select_info_list_view)
    ListViewForScrollView listview;
    @Bind(R.id.select_info_submit)
    TextView submit;
    @Inject
    SelectInfoPresenter presenter;
    @Inject
    SelectInfoBiz bia;
    @Bind(R.id.select_info_city_text)
    TextView businessText;

    WirelessAdp adp;

    List<String> stringList = new ArrayList<>();

    private ArrayList<CityInfo> options1Items = new ArrayList<>();
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();

    private ArrayList<CityInfo> type1Items = new ArrayList<>();
    private ArrayList<ArrayList<String>> type2Items = new ArrayList<>();
    private ArrayList<ArrayList<ArrayList<String>>> type3Items = new ArrayList<>();
    SettingInfo settingInfo = new SettingInfo();
    String type = "";


    @Override
    protected void setRoot() {
        setTitleBar("#40C85C");
        setContentView(R.layout.activity_select_info);
    }

    @Override
    protected void setup() {
        setToolBar("自定义筛选", "");
        needsx.setText("重 置");
        DaggerSetecltInfoComponent.builder().selectInfoModule(new SelectInfoModule(this)).build().inject(this);
        presenter.setBiz(bia);
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        intUI();
        presenter.getData();
        presenter.getStudy();
        RxView.clicks(submit)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        String studyInfo = "";
                        if (stringList.size() > 0) {
                            for (String string : stringList) {
                                studyInfo = studyInfo + string + ",";
                            }
                        } else {
                            studyInfo = "";
                        }
                        settingInfo.setTrainingType(studyInfo);
                        presenter.toMian(settingInfo, type);
                    }
                });

        RxView.clicks(business)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        ShowPickerView();
                    }
                });

        RxView.clicks(education)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        ShowStudyrView();
                    }
                });
        RxView.clicks(needsx)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        clear();
                    }
                });
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }


    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRemoveActEvent(RemoveInfoEvent event) {
        if (event.getPosition() < stringList.size() && event.getPosition() >= 0) {
            stringList.remove(event.getPosition());
            adp.notifyDataSetChanged();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bia.onDestory();
    }


    private void intUI() {
        Constants.registerActivity.add(this);
        adp = new WirelessAdp(this);
        listview.setAdapter(adp);
        type = StringUtils.isEmpty(getIntent().getStringExtra(Constants.KEYSTRING)) ? "" : getIntent().getStringExtra(Constants.KEYSTRING);
        String setInfosStr = PreferencesUtils.getStringPreferences("settingInfo",
                "settingInfo", null);
        if (!TextUtils.isEmpty(setInfosStr)) {
            SettingInfo settingInfo = new Gson().fromJson(setInfosStr,
                    new TypeToken<SettingInfo>() {
                    }.getType());
            MyApplication.settingInfo = settingInfo;
        }

        if (MyApplication.settingInfo == null) {
            settingInfo.setBusinessCircle("");
            settingInfo.setTrainingType("");
        } else {
            settingInfo.setBusinessCircle(StringUtils.isEmpty(MyApplication.settingInfo.getBusinessCircle()) ? "" : MyApplication.settingInfo.getBusinessCircle());
            settingInfo.setTrainingType(StringUtils.isEmpty(MyApplication.settingInfo.getTrainingType()) ? "" : MyApplication.settingInfo.getTrainingType());
        }
        businessText.setText(StringUtils.isEmpty(settingInfo.getBusinessCircle()) ? "不限" : settingInfo.getBusinessCircle());
        String[] all = settingInfo.getTrainingType().trim().split(",");

        for (int i = 0; i < all.length; i++) {
            if (all[i].length() > 0 && !StringUtils.isEmpty(all[i])) {
                stringList.add(all[i].substring(0, all[i].length()));
            }
        }
        adp.setData(stringList);
        adp.notifyDataSetChanged();
    }

    private void clear() {
        businessText.setText("不限");
        settingInfo.setBusinessCircle("");
        stringList.clear();
        adp.notifyDataSetChanged();
    }

    private void ShowPickerView() {
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {

                String info = options1Items.get(options1).getPickerViewText() + "-" +
                        options2Items.get(options1).get(options2) + "-" +
                        options3Items.get(options1).get(options2).get(options3);
                businessText.setText(info);
                settingInfo.setBusinessCircle(info);

            }
        }).setTitleText("商圈选择")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK)
                .setContentTextSize(20)
                .build();
        pvOptions.setPicker(options1Items, options2Items, options3Items);
        pvOptions.show();

    }

    private void ShowStudyrView() {
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String info = type1Items.get(options1).getPickerViewText() + "-" +
                        type2Items.get(options1).get(options2) + "-" +
                        type3Items.get(options1).get(options2).get(options3);
                if (stringList.size() >= 5) {
                    showMsg("最多选择5个教育类型");
                } else {
                    boolean isContain = false;
                    for (String string : stringList) {
                        if (info.equals(string)) {
                            showMsg("无法重复选择教育类型");
                            isContain = true;
                        }
                    }
                    if (!isContain) {
                        stringList.add(info);
                        adp.notifyDataSetChanged();
                    }
                }
            }
        }).setTitleText("教育类型")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK)
                .setContentTextSize(20)
                .build();
        pvOptions.setPicker(type1Items, type2Items, type3Items);
        pvOptions.show();

    }


    @Override
    public void getJson(ArrayList<CityInfo> jsonBean) {
//        for (CityInfo cityInfo : jsonBean) {
//            List<CityInfo.CityBean> cityBeanList = cityInfo.getCity();
//            CityInfo.CityBean cityBean = new CityInfo.CityBean();
//            cityBean.setName("不限");
//            List<String> areaList = new ArrayList<>();
//            areaList.add("不限");
//            cityBean.setArea(areaList);
//            cityBeanList.add(0, cityBean);
//
//            for (CityInfo.CityBean cityBean1 : cityBeanList) {
//                List<String> areaList1 = cityBean1.getArea();
//                if (!areaList1.contains("不限"))
//                    areaList1.add(0, "不限");
//            }
//        }

        options1Items = jsonBean;
        for (int i = 0; i < jsonBean.size(); i++) {
            ArrayList<String> CityList = new ArrayList<>();
            ArrayList<ArrayList<String>> Province_AreaList = new ArrayList<>();
            for (int c = 0; c < jsonBean.get(i).getCity().size(); c++) {
                CityList.add(jsonBean.get(i).getCity().get(c).getName());
                ArrayList<String> City_AreaList = new ArrayList<>();
                if (jsonBean.get(i).getCity().get(c).getArea() == null
                        || jsonBean.get(i).getCity().get(c).getArea().size() == 0) {
                    City_AreaList.add("");
                } else {
                    for (int d = 0; d < jsonBean.get(i).getCity().get(c).getArea().size(); d++) {//该城市对应地区所有数据
                        City_AreaList.add(jsonBean.get(i).getCity().get(c).getArea().get(d));
                    }
                }
                Province_AreaList.add(City_AreaList);
            }
            options2Items.add(CityList);
            options3Items.add(Province_AreaList);
        }
    }

    @Override
    public void getBussinessType(ArrayList<CityInfo> jsonBean) {
        type1Items = jsonBean;
        for (int i = 0; i < jsonBean.size(); i++) {
            ArrayList<String> CityList = new ArrayList<>();
            ArrayList<ArrayList<String>> Province_AreaList = new ArrayList<>();

            for (int c = 0; c < jsonBean.get(i).getCity().size(); c++) {
                String CityName = jsonBean.get(i).getCity().get(c).getName();
                CityList.add(CityName);

                ArrayList<String> City_AreaList = new ArrayList<>();

                if (jsonBean.get(i).getCity().get(c).getArea() == null
                        || jsonBean.get(i).getCity().get(c).getArea().size() == 0) {
                    City_AreaList.add("");
                } else {

                    for (int d = 0; d < jsonBean.get(i).getCity().get(c).getArea().size(); d++) {
                        String AreaName = jsonBean.get(i).getCity().get(c).getArea().get(d);

                        City_AreaList.add(AreaName);
                    }
                }
                Province_AreaList.add(City_AreaList);
            }
            type2Items.add(CityList);
            type3Items.add(Province_AreaList);
        }

    }
}


